package git.soulbgm.common.realm;

import git.soulbgm.mapper.RoleMenuMapper;
import git.soulbgm.pojo.*;
import git.soulbgm.service.*;
import git.soulbgm.utils.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import java.util.*;

/**
 * 自定义Realm
 *
 * @author SoulBGM
 * @version 1.0
 * @date 2019/10/12 14:51
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private LoginService service;

    /**
     * 用户状态 正常
     */
    private static int USER_STATE_NORMAL = 1;

    /**
     * 用户状态 锁定
     */
    private static int USER_STATE_LOCKING = 2;

    /**
     * 用户状态 删除
     */
    private static int USER_STATE_DELETE = 0;

    private LogUtil log = new LogUtil(getClass());

    @Override
    public String getName() {
        return "CustomRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    /**
     * 获取授权信息(查看用户权限)
     *
     * @param principals 主要信息
     * @return 授权信息(权限信息)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        Object primaryPrincipal = principals.getPrimaryPrincipal();
        if (primaryPrincipal instanceof PrincipalUser) {
            PrincipalUser user = (PrincipalUser) primaryPrincipal;
            simpleAuthorizationInfo.setRoles(user.getRoleSet());
            simpleAuthorizationInfo.setStringPermissions(user.getUrlSet());
        }
        return simpleAuthorizationInfo;
    }

    /**
     * 获取认证信息(登录)
     *
     * @param token 认证令牌(用户名/密码)
     * @return 认证信息(用户信息)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        UserDto user = service.findByUsername(userToken.getUsername());
        // 说明用户名不存在
        if (user == null) {
            throw new UnknownAccountException("用户不存在");
        }

        if (user.getState() == USER_STATE_DELETE) {
            throw new UnknownAccountException("用户已删除");
        }

        if (user.getState() == USER_STATE_LOCKING) {
            throw new LockedAccountException("用户已锁定");
        }

        PrincipalUser principalUser = new PrincipalUser();
        principalUser.setUsername(user.getUsername());
        principalUser.setId(user.getId());

        try {
            service.setRoleAndPermissions(principalUser);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return new SimpleAuthenticationInfo(principalUser, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
    }

}
